package com.jml.zlhq.app.ui;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.webkit.JavascriptInterface;

import com.jml.zlhq.app.BuildConfig;
import com.jml.zlhq.app.util.BaseTools;
import com.jml.zlhq.app.util.StringUtil;
import com.jml.zlhq.app.util.UIUtil;

public class WebAppInterface {
		private Activity mActivity;
		private webCallBack share,collect,info;

		/** Instantiate the interface and set the context */
		WebAppInterface(Activity mActivity,webCallBack share,webCallBack collect,webCallBack info) {
			this.mActivity = mActivity;
			this.share = share;
			this.collect = collect;
			this.info = info;
		}

		/** Show a toast from the web page */
		// 如果target 大于等于API 17，则需要加上如下注解window.thirdLogin.login(param)
		@JavascriptInterface
		public void method(String obj) {
			if(BuildConfig.DEBUG)Log.i("====js====", obj);
			if(StringUtil.isEmpty(obj))return;
			try {
				JSONObject json = new JSONObject(obj);
				String type = BaseTools.getFrom("type", json);
				if(type.equals("imgs")){
					int index = 0;
					ArrayList<String> list = new ArrayList<String>();
					index = json.getInt("imgindex");
					JSONArray jsonarr = json.get("data").toString().equals("null")?new JSONArray():json.getJSONArray("pics");
					for (int i = 0;jsonarr!=null&& i < jsonarr.length(); i++) {
						list.add(BaseTools.getFrom("image_url", jsonarr.getJSONObject(i)));
					}
					Intent intent = new Intent(mActivity, ViewPagerActivity.class);
					Bundle data = new Bundle();
					data.putSerializable("list", list);
					data.putInt("index", index);
					intent.putExtras(data);
					mActivity.startActivity(intent);
				}
				if(type.equals("showshop")){
					Bundle data = new Bundle();
					data.putString("url", BaseTools.getFrom("url",json));
					data.putString("id", BaseTools.getFrom("id",json));
					UIUtil.openActivity(mActivity, ShopDetailActivity.class, data);
				}
				if(type.equals("team")){
					Bundle data = new Bundle();
					data.putString("url", BaseTools.getFrom("url",json));
					data.putString("id", BaseTools.getFrom("id",json));
					UIUtil.openActivity(mActivity, TeamDetailActivity.class, data);
				}
				if(type.equals("work")){
					Bundle data = new Bundle();
					data.putString("url", BaseTools.getFrom("url",json));
					data.putString("id", BaseTools.getFrom("id",json));
					UIUtil.openActivity(mActivity, WorkDetailActivity.class, data);
				}
				if(type.equals("map")){
					Bundle data = new Bundle();
					data.putString("latitude", BaseTools.getFrom("latitude",json));
					data.putString("longitude", BaseTools.getFrom("longitude",json));
					//UIUtil.openActivity(mActivity, MapActivity.class, data);
				}
				if(type.equals("collection")){
					if(collect!=null)this.collect.call(null);
				}
				if(type.equals("share")){
					if(share!=null)this.share.call(null);
				}
				if(type.equals("information")){
					if(info!=null)this.info.call(obj);
				}
				if(type.equals("phone")){
					try {
						String phoneString=BaseTools.getFrom("phonenum", json);
						Intent intent=new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+phoneString));
//						intent.setAction("android.intent.action.CALL");
//						intent.addCategory("android.intent.category.DEFAULT");
						mActivity.startActivity(intent);
					} catch (Exception e) {
						UIUtil.ToastMessage(mActivity, "无法获取商家联系方式");
					}
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
//			if(type.equals("openlink_shop")){
//				Bundle data = new Bundle();
//				data.putString("url", id);
//				UIUtil.openActivity(mActivity, ShopDetailActivity.class, data);
//			}
//			if(type.equals("openlink_work")){
//				Bundle data = new Bundle();
//				data.putString("url", id);
//				UIUtil.openActivity(mActivity, ZuopingActivity.class, data);
//			}
			
		}
		
		@JavascriptInterface
		public void method(String type,String id) {
			if(BuildConfig.DEBUG)Log.i("====js====", type+","+id);
			if(StringUtil.isEmpty(type))return;
			if(type.equals("openlink_shop")){
				Bundle data = new Bundle();
				data.putString("url", id);
				UIUtil.openActivity(mActivity, ShopDetailActivity.class, data);
			}
			if(type.equals("openlink_work")){
				Bundle data = new Bundle();
				data.putString("url", id);
				UIUtil.openActivity(mActivity, WorkDetailActivity.class, data);
			}
			
		}
		
		@JavascriptInterface
		public void method(String type, String imgidx, String pics) {
			if(BuildConfig.DEBUG)Log.i("====js====", type+","+imgidx+","+pics);
			
			if(StringUtil.isEmpty(type))return;
			int index = 0;
			ArrayList<String> list = new ArrayList<String>();
			try {
				index = Integer.parseInt(imgidx);
				JSONArray jsonarr = new JSONArray(pics);
				for (int i = 0;jsonarr!=null&& i < jsonarr.length(); i++) {
					list.add(BaseTools.getFrom("image_url", jsonarr.getJSONObject(i)));
				}
			} catch (Exception e) {}
			if(type.equals("clickpicture")){
				Intent intent = new Intent(mActivity, ViewPagerActivity.class);
				Bundle data = new Bundle();
				data.putSerializable("list", list);
				data.putInt("index", index);
				intent.putExtras(data);
				mActivity.startActivity(intent);
//				Bundle data = new Bundle();
//				data.putSerializable("list", list);
//				data.putInt("index", index);
//				UIUtil.openActivity(mActivity, ViewPagerActivity.class, data);
			}
		}
		
		public interface webCallBack{
			public void call(Object obj);
		}
}